﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ForumsBrowser.BusinessObjects;
using ForumsBrowser.Common;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace ForumsBrowser
{
    /// <summary>
    /// Interaction logic for PopupAlert.xaml
    /// </summary>
    public partial class PopupAlert : Window
    {
        private static int _count = 0;
        private static object m_lock = new object();
        private int _myCount = 0;
        private event EventHandler<ThreadSelectedEventArgs> ViewThread;
        private Thread _thread;
        private static event EventHandler CauseFall;

        public static void Show(Thread thread, EventHandler<ThreadSelectedEventArgs> handler)
        {
            if (_count < Configuration.MaxPopupsCount && Configuration.PopupsEnabled)
            {
                int tempCount = ++_count;
                PopupAlert alert = new PopupAlert(thread, handler);
                alert.ShowActivated = false;
                alert._myCount = tempCount;
                alert.Show();
                CauseFall += alert.Fall;
            }
        }

        public void Fall(object sender, EventArgs e)
        {
            ((Storyboard)this.Resources["dropItem"]).Begin(this);
        }

        public TimeSpan? PopupDuration
        {
            get
            {
                return new TimeSpan(0, 0, Configuration.PopupDuration);
            }
        }

        private PopupAlert(Thread thread, EventHandler<ThreadSelectedEventArgs> handler)
        {
            try
            {
                InitializeComponent();
                this._thread = thread;
                this.DataContext = this._thread;
                this.ViewThread = handler;
            }
            catch
            {
                // nothing.
            }
        }

        void PopupAlert_Loaded(object sender, RoutedEventArgs e)
        {
            this.Top = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - (_myCount * 90);
            this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - this.ActualWidth;
            this.Topmost = true;
        }

        protected override void OnClosed(EventArgs e)
        {
            _count--;
            var handler = CauseFall;
            if (handler != null)
                handler(this, EventArgs.Empty);
            base.OnClosed(e);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            CauseFall -= this.Fall;
            base.OnClosing(e);
        }

        private void DoubleAnimation_Completed(object sender, EventArgs e)
        {
            CauseFall -= this.Fall;
            this.Close();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var handler = ViewThread;

            if (handler != null)
                handler(sender, new ThreadSelectedEventArgs { Thread = this._thread });
            this.Close();

            //if (Application.Current.MainWindow is MiniView)
            //    (Application.Current.MainWindow as MiniView).Close();
            //else if (Application.Current.MainWindow is MainWindow)
            //    Application.Current.MainWindow.WindowState = ((MainWindow)Application.Current.MainWindow).LastState;
        }
    }
}
